/*
 * rtc.c
 *
 * Created: 2013/12/25 15:39:10
 *  Author: alivehex
 */ 
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#include "uart.h"
#include "iolgt8f0xa.h"

// extern void ADCStart(void);

void RTCInit(int periodSecond) {
	unsigned long RTCTOP = 0;
	
	// enable the RTC clock
	PMCR |= (1 << 1);
	// wait for clock stable
	_delay_ms(1);
	
	// set the frequency
	RTCTOP = periodSecond * 128;
	RTCTOPL = RTCTOP & 0xFF;
	RTCTOPM = (RTCTOP >> 8) & 0xFF;
	RTCTOPH = (RTCTOP >> 16) & 0xFF;
	
	RTCNTL = 0;
	RTCNTM = 0;
	RTCNTH = 0;
	
	RTCSR = 0x80;
	// enable interrupt
	RTCSR |= (1 << 2);
	RTCISR = (1 << 0);
	
	while(!(RTCSR & 0x80));
	
	// enable the RTC clock
	RTCSR |= (1 << CWEN)|(1 << LOAD)|(1 << EN);
}

ISR(RTC_vect) {
	// ADCStart();
}
